#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from pathlib import Path
from scripts.set_environment import set_environment_based_on_ip,get_local_ip

BASE_DIR = Path(__file__).resolve().parent
SCRIPTS_ROOT = os.path.join(BASE_DIR, "scripts")

def main():
    """Run administrative tasks."""
    ip = get_local_ip()
    if ip:
        set_environment_based_on_ip(ip)
    else:
        print("Failed to set environment based on IP. Using default settings.")
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "root_backend.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError("Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?") from exc
    
    # import subprocess
    # print("Setting up the environment...")
    # print("Running set_environment.py...")
    # subprocess.run(['python', os.path.join(SCRIPTS_ROOT, 'set_environment.py')], check=True)
    print(os.environ.get("DJANGO_SETTINGS_MODULE"))
    
    execute_from_command_line(sys.argv)


if __name__ == "__main__":
    main()
